package cn.jingzhuan.fm.utils

import android.databinding.BindingAdapter
import android.widget.ImageView
import android.widget.TextView
import cn.jingzhuan.fm.ui.transformation.RoundedCornersTransformation

object DatabindingUtils {


    @JvmStatic
    @BindingAdapter("image")
    fun setImage(iv: ImageView, src: Int) {
        iv.setImageResource(src)
    }

    @JvmStatic
    @BindingAdapter("load_image","image_radius")
    fun loadImage(imageView: ImageView, url: String?, radius:Int?) {
        if (radius == null || radius <= 0) {
            ImageLoader.loadImage(imageView, url)
        } else {
            ImageLoader.loadImage(imageView, url, RoundedCornersTransformation(imageView.context,DisplayUtils.dip2px(imageView.context,radius.toFloat()),0, RoundedCornersTransformation.CornerType.ALL))
        }
    }

    @JvmStatic
    @BindingAdapter("number_format","number_format_digit")
    fun formatNumber(textView: TextView, value:String?, digit :Int?) {
        if (value != null && value?.length > 0) {
            if (digit != null) {
                textView.text = UnitParseUtils.parseNumber(value.toInt(),digit)
            } else {
                textView.text = UnitParseUtils.parseNumber(value.toInt(), 0)
            }
        }
    }


    @JvmStatic
    @BindingAdapter("number_to_time")
    fun numberToTime(textView: TextView, value:String?) {
        if (value != null && value?.length > 0) {
            var minute = value.toInt()/60
            var second = value.toInt() % 60
            var format = fun(value:Int):String {
                return if (value < 10 ) "0$value" else "$value"
            }
            textView.text = "${format(minute)}:${format(second)}"
        } else {
            textView.text = "--:--"
        }
    }


    @JvmStatic
    @BindingAdapter("load_blur_image")
    fun blurImage(view: ImageView, url: String?) {
        if (url != null && url?.length > 0) {
            ImageLoader.loadBlurImage(view, url)
        }
    }

    @JvmStatic
    @BindingAdapter("load_cricle_image")
    fun cricleImage(view: ImageView, url: String?) {
        ImageLoader.loadCircleImage(view,url)
    }
}



